package com.kc.sjwh.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.CzllyDao;
import com.kc.sjwh.dao.CzllzDao;
import com.kc.sjwh.dao.CzllzcyDao;
import com.kc.sjwh.entity.bo.CzllzBo;
import com.kc.sjwh.entity.bo.CzllzcyBo;
import com.kc.sjwh.entity.po.Czlly;
import com.kc.sjwh.entity.po.Czllz;
import com.kc.sjwh.entity.po.Czllzcy;
import com.kc.sjwh.entity.vo.dict.DictItem;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.other.Tool;

/**
 * 处置力量页 service
 * @author xianding
 * @date 2017年11月28日
 */
@Service
public class CzllyService {
	static Logger logger = Logger.getLogger(CzllyService.class);
	@Autowired
	CzllyDao czllyDao;
	@Autowired
	CzllzDao czllzDao;
	@Autowired
	CzllzcyDao czllzcyDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Czlly> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("t.gxsj desc");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("startTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj >= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("endTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t.gxsj <= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("t.ymc like ?".replace("?", "'%"+v+"%'"));
		}
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.put("t.ssdwdm", v);
		}
		
		return czllyDao.getPage("get", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Czlly one) {
		one.setCjsj(new Date());
		one.setYbh(Tool.getUuid());
		int num=czllyDao.count(new QueryParam().put("ssdwdm", one.getSsdwdm()));
		if (num>0 ) {
			throw new IllegalArgumentException("一个单位只允许一个页");
		}else {		
			czllyDao.add(one);
		}
	}

	/**
	 * 修改
	 * @param one
	 */
	public void update(Czlly one) {
		Czlly chk=czllyDao.getOne(new QueryParam().put("ssdwdm", one.getSsdwdm()));
		one.setGxsj(new Date());
		if (!one.getYbh().equals(chk.getYbh())) {
			throw new IllegalArgumentException("此单位已存在另一个页");
		}else {		
			czllyDao.update(one, "ybh");
		}
	}

	/**
	 * 删除
	 * @param id
	 */
	@Transactional
	public void delete(String id) {
		//级联删除 none
		Czlly one =new Czlly();
		one.setYbh(id);
		one.setSybz("N");
		one.setGxsj(new Date());
		czllyDao.update(one, "ybh");
		List<Czllz> list=czllzDao.getList(new QueryParam().put("ybh", id));
		for (Czllz czllz : list) {
			czllzcyDao.updateByZbh(czllz.getZbh());
		}
		czllzDao.updateByYbh(id);
		
//		//删除
//		int n = czllyDao.delete(id);
//		if(n == 0) {
//			throw new IllegalArgumentException("要删除的记录不存在");
//		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @return
	 */
	public Czlly form(String id) {
		return id==null ? null : czllyDao.getOne(new QueryParam().put("ybh", id));
	}

	/**
	 * 根据行设置处置力量页
	 */
//	public void setCzllyByRow(HSSFRow row, Czlly one) {
//		
//	}

	/**
	 * 取所有页为字典
	 * 
	 * @return
	 */
	public List<DictItem> getAllAsDict() {
		List<DictItem> list = new ArrayList<DictItem>();
		for (Czlly bean : czllyDao.getList(new QueryParam())) {
			DictItem di = new DictItem();
			di.setId(bean.getYbh());
			di.setName(bean.getYmc());
			list.add(di);
		}
		return list;
	}

	public List<DictItem> getList() {
		List<DictItem> list = new ArrayList<DictItem>();
		for (Czlly bean : czllyDao.getList(new QueryParam())) {
			DictItem di = new DictItem();
			di.setId(bean.getYbh());
			di.setName(bean.getYmc());
			di.setLev(1);
			list.add(di);
		}
		for (Czllz bean : czllzDao.getList(new QueryParam())) {
			CzllzBo bo =(CzllzBo) bean;
			DictItem di = new DictItem();
			di.setId(bo.getZbh());
			di.setName(bo.getZmc());
			di.setPid(bo.getYbh());
			di.setLev(2);
			list.add(di);
		}
		for (Czllzcy bean : czllzcyDao.getList(new QueryParam())) {
			CzllzcyBo bo =(CzllzcyBo) bean;
			DictItem di = new DictItem();
			di.setId(bo.getCybh());
			di.setName(bo.getCylxms()+"-"+bo.getCydxmc());
			di.setPid(bo.getZbh());
			di.setLev(3);
			list.add(di);
		}
		return list;
	}

}
